/**
 * Created by xsf on 11/23/2016.
 * 工具类
 */
define(function (require) {

    var Utilities = {
        getParameterByName: function (name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                results = regex.exec(location.search);
            return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        },
        findElement: function (arr, propName, propValue) {
            for (var i = 0; i < arr.length; i++)
                if (arr[i][propName] == propValue)
                    return arr[i];
        },
        findWithAttr: function (array, attr, value) {
            for (var i = 0; i < array.length; i += 1) {
                if (array[i][attr] === value) {
                    return i;
                }
            }
        },
        jsonObjToBase64: function (json) {
            return btoa(encodeURIComponent(JSON.stringify(json)));
        },
        base64TojsonObj: function (base64) {
            return JSON.parse(decodeURIComponent(atob(base64)));
        },
        pageJump: function (url) {
            location.href = url;
        },
        log: function () {
            for(key in arguments){
                console.log(JSON.parse(JSON.stringify(arguments[key])));
            }
        },
        copy: function (source) {
            var result = {};
            for (var key in source) {
                result[key] = typeof source[key] === 'object' ? this.copy(source[key]) : source[key];
            }
            return result;
        },
        params: function () {
            var url = window.location.search;
            if (url.indexOf("?") != -1) {
                var str = url.substr(1),
                    strs = str.split("&"),
                    key = new Array(strs.length),
                    value = new Array(strs.length),
                    params = {};
                for (var i = 0; i < strs.length; i++) {
                    key[i] = strs[i].split("=")[0]
                    value[i] = unescape(strs[i].split("=")[1]);
                    params[key[i]] = value[i]
                }
                return params;
            }
        },
        getTime: function (date) {
            var time = new Date(date);
            return {
                year: time.getFullYear(),
                month: (time.getMonth() + 1) < 10 ? '0' + (time.getMonth() + 1) : (time.getMonth() + 1),
                day: time.getDate() < 10 ? '0' + time.getDate() : time.getDate(),
                hh: time.getHours(),
                mm: time.getMinutes()
            };
        }
    };
    Utilities.routeParams = Utilities.params();
    return Utilities;

});